import request from "@/utils/request";
import type {
  GetSpuListParams,
  GetSpuListResponse,
  BaseSaleAttrList,
  AddOrUpdateSpuParams,
  SpuImageList,
  SpuSaleAttrList,
} from "./models/spuModels";

enum Api {
  getSpuListUrl = `/admin/product`,
  getBaseSaleAttrListUrl = `/admin/product/baseSaleAttrList`,
  addSpuUrl = `/admin/product/saveSpuInfo`,
  getSpuImageListUrl = `/admin/product/spuImageList`,
  getSpuSaleAttrListUrl = `/admin/product/spuSaleAttrList`,
  updateSpuUrl = `/admin/product/updateSpuInfo`,
}

/**
 * 获取SPU列表
 * @param GetSpuListParams
 * @returns GetSpuListResponse
 */
export const getSpuListApi = ({
  page,
  limit,
  category3Id,
}: GetSpuListParams) => {
  return request.get<any, GetSpuListResponse>(
    Api.getSpuListUrl + `/${page}/${limit}`,
    {
      // query参数，查询字符串参数
      params: {
        category3Id,
      },
    }
  );
};

/**
 * 获取基础销售属性列表
 * @returns BaseSaleAttrList
 */
export const getBaseSaleAttrListApi = () => {
  return request.get<any, BaseSaleAttrList>(Api.getBaseSaleAttrListUrl);
};

/**
 * 添加SPU
 * @param spu AddOrUpdateSpuParams
 * @returns null
 */
export const addSpuApi = (spu: AddOrUpdateSpuParams) => {
  return request.post<any, null>(Api.addSpuUrl, spu);
};

/**
 * 获取图片列表
 * @param spuId spu的id
 * @returns SpuImageList
 */
export const getSpuImageListApi = (spuId: number) => {
  return request.get<any, SpuImageList>(Api.getSpuImageListUrl + `/${spuId}`);
};

/**
 * 获取销售属性
 * @param spuId spu的id
 * @returns SpuSaleAttrList
 */
export const getSpuSaleAttrListApi = (spuId: number) => {
  return request.get<any, SpuSaleAttrList>(
    Api.getSpuSaleAttrListUrl + `/${spuId}`
  );
};

/**
 * 更新SPU
 * @param spu AddOrUpdateSpuParams
 * @returns null
 */
export const updateSpuApi = (spu: AddOrUpdateSpuParams) => {
  return request.post<any, null>(Api.updateSpuUrl, spu);
};
